<template>
    <el-main>
        <el-form :model="searchModel" ref="searchForm" label-width="80px" :inline="true" size="small">
            <el-form-item>
                <el-input v-model="searchModel.key" placeholder="请输入" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" plain icon="el-icon-search" @click="search(pageNo, pageSize)">查询
                </el-button>
                <el-button icon="el-icon-refresh-right" @click="resetValue">重置</el-button>
                <el-button type="success" plain icon="el-icon-plus" @click="openAddWindow()">新增</el-button>
            </el-form-item>
        </el-form>
        <!-- 数据表格 -->
        <el-table :data="noticeList" :height="tableHeight" border stripe style="width: 100%; margin-bottom: 10px">
            <el-table-column prop="id" label="序号" width="100" align="center"></el-table-column>
            <el-table-column prop="title" label="标题"></el-table-column>
            <el-table-column prop="content" label="内容"></el-table-column>
            <el-table-column prop="createAt" label="创建时间"></el-table-column>
            <el-table-column prop="userName" label="创建人"></el-table-column>

            <el-table-column label="操作" align="center" width="240">
                <template slot-scope="scope">
                    <el-button icon="el-icon-edit" type="primary" plain size="small" @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button icon="el-icon-delete" type="danger" plain size="small"
                        @click="handleDelete(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页工具栏 -->
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="pageNo" :page-sizes="[10, 20, 30, 40, 50]" :page-size="10"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 添加和修改公告窗口 -->
        <system-dialog :title="noticeDialog.title" :visible="noticeDialog.visible" :width="noticeDialog.width"
            :height="noticeDialog.height" @onClose="onClose" @onConfirm="onConfirm">
            <div slot="content">
                <el-form :model="notice" ref="noticeForm" :rules="rules" label-width="80px" :inline="false"
                    size="small">
                    <el-form-item label="标题" prop="title">
                        <el-input type="text" v-model="notice.title"></el-input>
                    </el-form-item>
                    <el-form-item label="内容" prop="content">
                        <el-input type="text" v-model="notice.content"></el-input>
                    </el-form-item>
                    <el-form-item label="创建时间" prop="createAt">
                        <el-date-picker v-model="notice.createAt" type="date" placeholder="请选择日期" format="yyyy/MM/dd"
                            value-format="yyyy-MM-dd hh:mm:ss" />
                    </el-form-item>
                    <el-form-item label="创建人" prop="userId">
                        <el-select placeholder="请选择员工" clearable v-model="notice.userId">
                            <el-option v-for="item in employeeOptions" :key="item.id" :label="item.username"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
        </system-dialog>

    </el-main>
</template>

<script>
import noticeApi from "@/api/notice";
import userApi from "@/api/user";
//导入对话框组件
import SystemDialog from "@/components/system/SystemDialog.vue";
export default {
    name: "noticeList",
    components: {
        SystemDialog,
    },
    data() {
        return {
            //查询条件
            searchModel: {
                key: "",
                pageNo: 1,
                pageSize: 10,
                userIds: []
            },
            employeeOptions: [],
            rules: {
                title: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
                content: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
                createAt: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
                userId: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],

            },
            //添加和修改公告窗口属性
            noticeDialog: {
                title: "",
                visible: false,
                height: 230,
                width: 500,
            },
            // 对象
            notice: {
                id: "",
                title: "",
                content: "",
                createAt: "",
                userId: null,
                userName: "",
                createUser: this.$store.getters.userId,
            },
            noticeList: [], //数据列表
            deptions: [],
            tableHeight: 0, //表格高度
            pageNo: 1, //当前页码
            pageSize: 10, //每页显示数量
            total: 0, //总数量
        };
    },
    methods: {
        /**
         * 查询
         */
        async search(pageNo = 1, pageSize = 10) {
            this.searchModel.pageNo = pageNo; //当前页码
            this.searchModel.pageSize = pageSize; //每页显示数量
            //发送查询请求
            let res = await noticeApi.NoticeList(this.searchModel);
            //执行成功
            if (res.success) {
                //公告列表
                this.noticeList = res.data.records;
                //总数量
                this.total = res.data.total;
            }
        },
        /**
          * 获取员工名单
          */
        async getStaff() {
            //发送查询请求
            let res = await userApi.getUserAllList({});
            //执行成功
            if (res.success) {
                console.log(res)
                //员工列表
                this.employeeOptions = res.data;
            }
        },

        /**
         * 重置查询条件
         */
        resetValue() {
            //清空查询条件
            this.searchModel.key = null;
            //重新查询
            this.search();
        },
        /**
         * 当每页数量发生变化时触发该事件
         */
        handleSizeChange(size) {
            this.pageSize = size; //将每页显示的数量交给成员变量
            this.search(this.pageNo, size);
        },
        /**
         * 当页码发生变化时触发该事件
         */
        handleCurrentChange(page) {
            this.pageNo = page;
            //调用查询方法
            this.search(page, this.pageSize);
        },
        /**
         * 打开添加窗口
         */
        openAddWindow() {
            //清空表单数据
            this.$resetForm("noticeForm", this.notice);
            this.noticeDialog.title = "新增公告信息"; //设置窗口标题
            this.noticeDialog.visible = true; //显示窗口
        },
        /**
         * 窗口取消事件
         */
        onClose() {
            this.noticeDialog.visible = false;
        },
        handleEdit(row) {
            //数据回显
            this.$objCopy(row, this.notice); //将当前编辑的数据复制到notice对象中
            //设置窗口标题
            this.noticeDialog.title = "编辑公告信息";
            //显示编辑公告窗口
            this.noticeDialog.visible = true;
        },

        /**
            * 新增或编辑确认事件
            */
        onConfirm() {
            this.$refs.noticeForm.validate(async (valid) => {
                if (valid) {
                    let res = null;
                    //判断用户ID是否为空
                    res = await noticeApi.NoticeSave(this.notice);
                    //判断是否成功
                    if (res.success) {
                        this.$message.success(res.message);

                        //关闭窗口
                    } else {
                        this.$message.error(res.message);

                    }
                    this.noticeDialog.visible = false;
                    this.search();
                }
            });
        },
        /**
* 删除
 
*/

        async handleDelete(row) {
            //查询部门下是否存在子部门或用户
            let res = await noticeApi.NoticeDel({ mainIds: [row.id] });
            //判断是否可以删除
            if (res.success) {
                //成功提示
                this.$message.success(res.message);
                //刷新
                this.search();
            } else {
                //失败提示
                this.$message.error(res.message);
            }
        }


    },

    created() {
        //调用查询公告列表的方法
        this.search();

    },
    mounted() {
        this.$nextTick(() => {
            this.tableHeight = window.innerHeight - 220;
            this.getStaff();
        });
    },
};
</script>

<style></style>